package design;



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;


import logik.Hotel;
import logik.Hotelverwaltung;
import logik.Tester;
import logik.Testerverwaltung;

public class HotelJList extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JPanel panel1 = new JPanel();
	JPanel panel2 = new JPanel();
	JPanel panel3 = new JPanel();
	private JButton schreiben;
	private JButton abbrechen;
	public Hotelverwaltung verwaltung;
	public ArrayList<Hotel> verwaltung1;
	private DefaultListModel<Hotel> model1 = new DefaultListModel<>();

	
	private int y=150;
	private int x=650;
	
	public HotelJList(Hotelverwaltung v)
	{
		verwaltung = v;
		initComponents();
		setSize(600,450);
		setLocationRelativeTo(null);
		this.setVisible(true);
		setTitle("Hotels");
		//taAusgabe1.setPreferredSize(new Dimension(450,600));
		//setBounds(100, 100, 174, 300);
		//sp.setVisible(true);
		setResizable(false);
		setLocation(x,y);
		abbrechen.addActionListener(this);
		schreiben.addActionListener(this);
		aktualisieren1();
	}
	





	public void aktualisieren1()
	{
		model1.clear();
		for(Hotel h: verwaltung.getHliste())
		{
			model1.addElement(h);
		}
	}

	private void initComponents() 
	{
		final Container c = this.getContentPane();
		c.setLayout(new BorderLayout());
		c.setBackground(Color.CYAN);
		
		panel1 = new JPanel();
		panel1.setBorder(new EmptyBorder(5, 5, 0, 0));
		panel1.setLayout(new BorderLayout(0, 0));
		c.add(panel1);
		setTitle("HotelJList");
		
		
		JScrollPane scrollPane = new JScrollPane();
		final JList<Hotel> list = new JList<Hotel>(model1);
		scrollPane.setViewportView(list);
		panel1.add(scrollPane, BorderLayout.CENTER);
		
		schreiben = new JButton("schreiben");
		abbrechen = new JButton("abbrechen");
		panel1.add(panel2, BorderLayout.SOUTH);
		panel2.add(panel3);
		
		panel3.setLayout(new GridLayout(0,2));
		panel3.setBackground(Color.BLACK);
		panel3.add(schreiben);
		panel3.add(abbrechen);
		
		
		try
		{
			
			MouseListener mouseListener = new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() == 2) {
						int index = list.locationToIndex(e.getPoint());
						System.out.println("Double clicked on Item " + index);
						//TesterJList jl = new TesterJList(testerverwaltung.getliste1(testerverwaltung));
						//System.out.println(testerverwaltung.getliste1(testerverwaltung liste));

					}
				}
				
		 }; list.addMouseListener(mouseListener);
		} catch (NullPointerException e) 
		 {
			
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "Achtung");
		 }	

			
			
	}
	



	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		JButton source= (JButton)arg0.getSource();
		
		if(source==abbrechen)
		{
			dispose();
		}
		
			
		
		
	}
	
	public Hotelverwaltung getver()
	{
		return verwaltung;
	}

}
